package com.xudow.app.dynamicstate_old.module.message;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jude.beam.bijection.RequiresPresenter;
import com.jude.beam.expansion.BeamBaseActivity;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.jude.easyrecyclerview.decoration.DividerDecoration;
import com.jude.utils.JUtils;
import com.xudow.app.R;
import com.xudow.app.dynamicstate_old.domain.entity.PushMessage;
import com.xudow.app.dynamicstate_old.widget.ItemSlideHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by ske on 2016/7/6.
 */
@RequiresPresenter(SystemMsgPresenter.class)
public class SystemMsgActivity extends BeamBaseActivity<SystemMsgPresenter> {
    @BindView(R.id.clear)
    TextView clear;
    @BindView(R.id.recycler)
    EasyRecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_msg);
        ButterKnife.bind(this);
        setupView();
    }

    private SlideItemAdapter adapter;

    private void setupView() {
        $(R.id.back_img).setOnClickListener(v -> finish());
        recyclerView.addItemDecoration(new DividerDecoration(getResources().getColor(R.color.gray100), JUtils.dip2px(1)));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter = new SlideItemAdapter(this));
        recyclerView.setEmptyView(R.layout.beam_view_list_con_empty);
        recyclerView.setRefreshListener(getPresenter());
        recyclerView.setErrorView(R.layout.beam_view_list_error);
        adapter.setMore(R.layout.beam_view_list_more, getPresenter());
        adapter.setNoMore(R.layout.list_no_more);
        adapter.setError(R.layout.beam_view_error);

        clear.setOnClickListener(v -> {
            if (getAdapter().getCount() > 0) {
                getPresenter().clearMsg();
            }
        });
    }

    public void stopRefresh() {
        recyclerView.setRefreshing(false);
    }

    public SlideItemAdapter getAdapter() {
        return adapter;
    }

    class SlideItemAdapter extends RecyclerArrayAdapter<PushMessage> implements ItemSlideHelper.Callback {
        private RecyclerView mRecyclerView;

        public SlideItemAdapter(Context context) {
            super(context);
        }

        @Override
        public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
            SystemMsgViewHolder viewHolder = new SystemMsgViewHolder(parent);
            viewHolder.setOnDeleteListener((position, mid) -> getPresenter().delMsg(position, mid));
            return viewHolder;
        }

        @Override
        public void onAttachedToRecyclerView(RecyclerView recyclerView) {
            super.onAttachedToRecyclerView(recyclerView);

            mRecyclerView = recyclerView;
            mRecyclerView.addOnItemTouchListener(new ItemSlideHelper(mRecyclerView.getContext(), this));
        }

        @Override
        public int getHorizontalRange(RecyclerView.ViewHolder holder) {
            if (holder.itemView instanceof LinearLayout) {
                ViewGroup viewGroup = (ViewGroup) holder.itemView;
                if (viewGroup.getChildCount() == 2) {
                    return viewGroup.getChildAt(1).getLayoutParams().width;
                }
            }
            return 0;
        }

        @Override
        public RecyclerView.ViewHolder getChildViewHolder(View childView) {
            return mRecyclerView.getChildViewHolder(childView);
        }

        @Override
        public View findTargetView(float x, float y) {
            return mRecyclerView.findChildViewUnder(x, y);
        }
    }
}
